#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDebug>
#include <QPushButton>
#include <QProcess>
#include <QDesktopWidget>
#include <QDesktopWidget>
#include <QScrollArea>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent, Qt::FramelessWindowHint),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    QHBoxLayout *mainlayout = new QHBoxLayout;
    mainlayout->addWidget(ui->tabWidget);
    mainlayout->setMargin(20);
    ui->centralWidget->setLayout(mainlayout);
    initUI();
    connects();
}
MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::createStatusBar()
{
    time = new TimeWidget(this);
    scrollingMessageDisplay = new ScrollingMessageDisplayWidget(this,85);
    volumeLimiter = new VolumeLimiterWidget(this);
    QWidget *lockButton_w = new QWidget;
    lockButton_w->setMinimumSize(50,40);
    lockButton_w->setMaximumSize(50,40);
    lockButton = new QPushButton(trUtf8("锁定"), lockButton_w);
    lockButton->setGeometry(0,0,50,40);
    lockButton->setMinimumSize(50,40);
    lockButton->setMaximumSize(50,40);
    ui->statusBar->addPermanentWidget(time,1);
    ui->statusBar->addPermanentWidget(scrollingMessageDisplay,20);
    ui->statusBar->addPermanentWidget(volumeLimiter,1);
    ui->statusBar->addPermanentWidget(lockButton_w,1);


    connect(lockButton,SIGNAL(clicked()),this,SLOT(lockSys()));
}
void MainWindow::initUI()
{
    createCentreGatewayWidget();
    createZigBeeWidget();
    createRemoteControlWidget();
    createHistoryControlData();
    createStatusBar();
    createSetPaWidget();
}

void MainWindow::connects()
{
    connect(centreGateway,SIGNAL(activatedZIgBee(QString)),this,SIGNAL(sig_activatedZIgBee(QString)));
    connect(centreGateway,SIGNAL(activatedGSM(QString)),this,SIGNAL(sig_activatedGSM(QString)));
    connect(centreGateway,SIGNAL(setGSMworkStatus(QString,bool)),this,SIGNAL(sig_setGSMworkStatus(QString,bool)));
    connect(centreGateway,SIGNAL(setZigBeeWorkStatus(QString,bool)),this,SIGNAL(sig_setZigBeeWorkStatus(QString,bool)));
    connect(realData,SIGNAL(sig_aFilter(Filter)),this,SIGNAL(sig_aFilter(Filter)));
    connect(remoteControl, SIGNAL(sig_message(Message)),this, SIGNAL(sig_sendMessage(Message)));
}
void MainWindow::showWindow()
{
    show();
#ifdef TEST_NET
    emit sig_show();
#endif
    //scrollingMessageDisplay->setMessage("qqqqqqqqqqqq");
}
void MainWindow::createCentreGatewayWidget()
{
    QScrollArea *area = new QScrollArea();
    area->setWidgetResizable(true);
    QHBoxLayout *layout = new QHBoxLayout;
    centreGateway = new CentreGatewayWidget;
    area->setWidget(centreGateway);
    layout->addWidget(area);
    ui->tabWidget->widget(0)->setLayout(layout);


    connect(centreGateway,SIGNAL(shutDownSystem()),this,SLOT(quitSys()));
    connect(centreGateway,SIGNAL(lockSystem()),this,SLOT(lockSys()));
    connect(centreGateway,SIGNAL(restartSystem()),this,SLOT(rebootSys()));
}
void MainWindow::quitSys()
{
    writeFile();
    qApp->quit();
}
void MainWindow::lockSys()
{
    hide();
    LockFullScreenWidget *lfsw = new LockFullScreenWidget;
    lfsw->setWindowFlags(Qt::FramelessWindowHint);
    LockSystemDialog lsd;
    lsd.raise();
    connect(&lsd,SIGNAL(unlockSuccess()),lfsw,SLOT(closeWidget()));
    connect(lfsw,SIGNAL(destroyed()),this,SLOT(show()));
    lfsw->showWidget();
    lsd.move((QApplication::desktop()->width() - lsd.width())/2,(QApplication::desktop()->height() - lsd.height())/2);
    lsd.show();
    lsd.exec();

    //    QScopedPointer<LockSystemDialog> lsd(new LockSystemDialog(this));
    //    lsd->setWindowFlags(Qt::FramelessWindowHint);
    //    lsd->setModal(true);
    //    lsd->move((QApplication::desktop()->width()-lsd->width())/2,
    //              (QApplication::desktop()->height()-lsd->height())/2);
    //    connect(lsd.take(),SIGNAL(unlockSuccess()),lfsw,SLOT(closeWidget()));
    //    lsd->show();
    //    lsd->exec();

}
void MainWindow::rebootSys()
{
    writeFile();
    qApp->quit();
    QProcess::startDetached(qApp->applicationFilePath(), QStringList() << "-qws");
}
void MainWindow::createZigBeeWidget()
{
    QScrollArea *area1 = new QScrollArea();
    area1->setWidgetResizable(true);
    realData = new RealDataWidget;
    QHBoxLayout *layout1 = new QHBoxLayout;
    area1->setWidget(realData);
    layout1->addWidget(area1);
    ui->tabWidget_2->widget(0)->setLayout(layout1);

    QScrollArea *area2 = new QScrollArea();
    area2->setWidgetResizable(true);
    historyData = new HistoryDataWidget;
    QHBoxLayout *layout2 = new QHBoxLayout;
    area2->setWidget(historyData);
    layout2->addWidget(area2);
    ui->tabWidget_2->widget(1)->setLayout(layout2);


    QHBoxLayout *layout3 = new QHBoxLayout;
    layout3->addWidget(ui->tabWidget_2);
    ui->tabWidget->widget(1)->setLayout(layout3);


}
void MainWindow::createRemoteControlWidget()
{
    QScrollArea *area1 = new QScrollArea();
    area1->setWidgetResizable(true);
    remoteControl = new RemoteControlWidget;
    QHBoxLayout *layout1 = new QHBoxLayout;
    area1->setWidget(remoteControl);
    layout1->addWidget(area1);
    ui->tabWidget_3->widget(0)->setLayout(layout1);

    QHBoxLayout *layout2 = new QHBoxLayout;
    layout2->addWidget(ui->tabWidget_3);
    ui->tabWidget->widget(2)->setLayout(layout2);
}
void MainWindow::createHistoryControlData()
{
    QScrollArea *area = new QScrollArea();
    area->setWidgetResizable(true);
    historyControlData = new HistoryControlDataWidget;
    QHBoxLayout *layout = new QHBoxLayout;
    area->setWidget(historyControlData);
    layout->addWidget(area);
    ui->tabWidget_3->widget(1)->setLayout(layout);
}

void MainWindow::slt_sensorData(const QString& adress, int value)
{
    realData->slt_lineGrap(adress,value);
}
void MainWindow::slt_netTopology(const QString &value)
{
    realData->slt_netTopologyBuild(value);
}

void MainWindow::slt_getGSMinfo(const QString &fileName, bool onOff)
{
    centreGateway->getGSMinfo(fileName,onOff);

}
void MainWindow::slt_getZigBeeInfo(const QString &fileName, bool onOff)
{
    centreGateway->getZigBeeInfo(fileName,onOff);

}
void MainWindow::slt_sensorData(ushort name, int flag, int value)
{
    if (flag == 0x01) {
        realData->slt_lineGrap(name,value);
    }
    realData->slt_updataNodeValue(name,value);

}

void MainWindow::slt_setListItems(const QString &adress, int name, int value, int flag)
{
    realData->slt_setListItems(adress,name,value,flag);
}
void MainWindow::slt_newNode(const Node &n)
{
    realData->slt_newNode(n);
}
//void MainWindow::slt_oldNode(const Node &n)
//{
//    realData->slt_oldNode(n);
//}
//void MainWindow::slt_nodeChange(ushort name, bool states)
//{
//    realData->slt_nodeChange(name,states);
//}
void MainWindow::slt_ChangNodeState(const Node &n)
{
    realData->slt_ChangNodeState(n);

}

void MainWindow::slt_ChangNodeState(int type,const QString &addr, const QString &paddr, ushort _name, bool status)
{
    realData->slt_ChangNodeState(type,addr,paddr,_name,status);
}

//void MainWindow::slt_updataNodeValue(ushort name, int value)
//{
//    realData->slt_updataNodeValue(name,value);
//}

void MainWindow::slt_printInfo(const QString &info)
{
    scrollingMessageDisplay->setMessage(info);
}

void MainWindow::createSetPaWidget()
{
    QScrollArea *area1 = new QScrollArea();
    area1->setWidgetResizable(true);
    SetPaWidget *w = new SetPaWidget;
    QHBoxLayout *layout1 = new QHBoxLayout;
    area1->setWidget(w);
    layout1->addWidget(area1);
    ui->tabWidget->widget(3)->setLayout(layout1);
}

void MainWindow::writeFile()
{
    QFile file("conf/Parameters.conf");
    if (file.open(QIODevice::ReadWrite)) {
        QTextStream out(&file);
        out << Parameters::cmsaddr.mid(7,4) << '\n'
            <<Parameters::phone.mid(3) << '\n'
           <<Parameters::interval_time << '\n'
          << Parameters::interval << '\n'
          << Parameters::TestDuration << '\n'
          << Parameters::boundaryValue << '\n'
          <<Parameters::spaceTime << '\n';
    }
    file.close();
}

